//
//  BRBannerDotView.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/29.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRBannerDotView.h"

@interface BRBannerDotView ()
{
    NSInteger _count;
    UIStackView *_stackView;
}
@end

@implementation BRBannerDotView

- (instancetype)initWithFrame:(CGRect)frame count:(NSInteger)count
{
    self = [super initWithFrame:frame];
    if (self) {
        _count = count;
        [self setUI];
    }
    return self;
}

- (instancetype)initWithCount:(NSInteger)count
{
    self = [super init];
    if (self) {
        _count = count;
        [self setUI];
    }
    return self;
}

- (void)selectItem:(NSInteger)index {
    if (index >= _count) return;
    for (int i = 0; i < _count; i++) {
        
        BRDotView *dot = (BRDotView *)[_stackView viewWithTag:100+i];
        if (i == index) {
            dot.sizeType = BRDotSize_Big;
        }else if (i == (index - 1) || i == (index + 1) ) {
            dot.sizeType = BRDotSize_Middle;
        }else {
            dot.sizeType = BRDotSize_Small;
        }
    }
}

- (void)setUI {
    
    /// 默认16
    /// 最大 8 6 4
    UIStackView *stackView = [[UIStackView alloc] init];
    stackView.axis = UILayoutConstraintAxisHorizontal;
    stackView.alignment = UIStackViewAlignmentFill;
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.spacing = 0;
    [self addSubview:stackView];
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(16.0);
        make.width.mas_equalTo(16.0 * _count);
        make.centerY.centerX.equalTo(self).offset(0);
    }];
    _stackView = stackView;
    
    for (int i = 0; i < _count; i++) {
        BRDotView *dot = [[BRDotView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
        [stackView addArrangedSubview:dot];
        dot.tag = 100 + i;
    }
    
    [self selectItem:0];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@interface BRDotView ()
{
    UIView  *_dot;
}
@end

@implementation BRDotView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUI];
    }
    return self;
}

- (void)setSizeType:(BRDotSize)sizeType {
    _sizeType = sizeType;
    CGFloat sizeW = 4 + sizeType * 2;
//    if (sizeType == BRDotSize_Middle) sizeW = 6;
//    if (sizeType == BRDotSize_Small) sizeType = 4;
    if (_dot.mj_h == sizeType) return;
    CGFloat dot_x = (16 - sizeW) /2;
    [UIView animateWithDuration:0.25 animations:^{
        self->_dot.mj_h = sizeW;
        self->_dot.mj_w = sizeW;
        self->_dot.mj_x = dot_x;
        self->_dot.mj_y = dot_x;
        self->_dot.layer.cornerRadius =  sizeW / 2.0;
    }];
//    _dot.center = self.center;
}

- (void)setUI {
    
    UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];
    dot.backgroundColor = UIColor.qd_lineColor4;
    dot.layer.masksToBounds = YES;
    dot.layer.cornerRadius = 4;
    dot.center = self.center;
    [self addSubview:dot];
    _dot = dot;
    
}


@end
